package com.znz.analysis.bean;

import java.io.File;
import java.io.IOException;

import com.maxmind.geoip2.DatabaseReader;

/**
 * @author 作者 :TP
 * @version 创建时间：2017年6月22日 上午11:35:44
 *
 */
public class GeoLite2DataBaseReader {
	private DatabaseReader databaseReader;
	// Singleton
	private volatile static GeoLite2DataBaseReader geoLite2DataBaseReader;

	public DatabaseReader getDatabaseReader() {
		return databaseReader;
	}

	public void setDatabaseReader(DatabaseReader databaseReader) {
		this.databaseReader = databaseReader;
	}

	private GeoLite2DataBaseReader() {
		init();
	}

	/**
	 * 初始化构造函数时执行
	 */
	private void init() {
		File database = new File("/usr/local/spark/spark-1.4.1-bin-hadoop2.6/data/GeoLite2-City.mmdb");
		try {
			DatabaseReader reader = new DatabaseReader.Builder(database).build();
			this.setDatabaseReader(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取单例对象
	 * 
	 * @return
	 */
	public static GeoLite2DataBaseReader getInstance() {
		// double check
		if (geoLite2DataBaseReader == null) {
			synchronized (GeoLite2DataBaseReader.class) {
				if (geoLite2DataBaseReader == null) {
					geoLite2DataBaseReader = new GeoLite2DataBaseReader();
				}
			}
		}
		return geoLite2DataBaseReader;
	}
}
